package com.core.registrar;

import com.core.annotation.Command;
import com.core.handler.CommandHandler;
import com.core.util.LogUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化注册到bean中
 *
 * @author hc
 * @CreateTime 2025/7/29 11:49
 */
@Component
public class CommandRegistrar {
    protected final Logger                      logger = LogUtil.getLogger(this.getClass());
    @Inject
    private         Map<String, CommandHandler> handlers;

    private final Map<String, CommandHandler> commandMap = new HashMap<>();

    @Init
    public void init() {
        if (handlers != null) {
            for (CommandHandler handler : handlers.values()) {
                Command anno = handler.getClass().getAnnotation(Command.class);
                if (anno != null) {
                    String cmd = anno.value();
                    commandMap.put(cmd, handler);
                    LogUtil.info(logger, "[注册命令] {} -> {}", cmd, handler.getClass().getSimpleName());
                }
            }
        }
    }

    public CommandHandler getHandler(String command) {
        return commandMap.get(command);
    }
}
